package cn.com.thinker.core.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@ConfigurationProperties(prefix="swagger2")
@PropertySource(value="classpath:application-swagger2.yml",encoding="UTF-8")
public class Swagger2Config {
	
	@Autowired
    Environment env;
	
	private String basePackage;
	private String title ;
	private String description ;
	private String termsOfServiceUrl ;
	private Contact contact;
	private String version;
	

	@Bean
    public Docket createRestApi() {
		
		init();
		
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }
	
    private ApiInfo apiInfo() {
 	
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(contact)
                .version(version)
                .build();
    }
    
    private void init(){
    	
    	this.basePackage = env.getProperty("basePackage");
		this.title = env.getProperty("title");
		this.description = env.getProperty("description");
		this.termsOfServiceUrl = env.getProperty("termsOfServiceUrl");
		this.contact = new Contact(env.getProperty("name"), env.getProperty("url"), env.getProperty("email"));
		this.version = env.getProperty("version");
    }

}
